<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="myTags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="/resources/css/common.css">
<script type="text/javascript" src="/resources/js/jquery.js"></script>
<script type="text/javascript">
function updateUser(targetId) {
	$('*[id='+targetId+']').each(function (i, e) {
		$('#updateUserForm').append($('<input/>', {
			type: 'hidden',
			name: e.name,
			value: e.value
		}));
	});
	$('form[id=updateUserForm] > input[name="id"]').attr({value:targetId});
	$('#updateUserForm').attr({action:'user/update', method:'post'}).submit();
}
function deleteUser(targetId) {
	$('form[id=deleteUserForm] > input[name="id"]').attr({value:targetId});
	$('#deleteUserForm').attr({action:'user/delete', method:'post'}).submit();
}
</script>
<title>User</title>
</head>
<body>
<jsp:include page="../common/gnb.jsp"/>
<jsp:include page="../common/header.jsp"/>

<table>
<c:forEach items="${userList}" var="row" varStatus="status">
<tr>
<td><input type="text" id="${row.id}" name="loginId" value="${row.loginId}"/></td>
<td><input type="password" id="${row.id}" name="password" value="${row.password}"/></td>
<td><input type="text" id="${row.id}" name="email" value="${row.email}"/></td>
<td><input type="text" id="${row.id}" name="firstName" value="${row.firstName}"/></td>
<td><input type="text" id="${row.id}" name="lastName" value="${row.lastName}"/></td>
<td>
<select id="${row.id}" name="groupId">
<option value="">None</option>
<c:forEach items="${groupList}" var="group" varStatus="status">
<option value="${group.id}" ${row.groupId == group.id ? 'selected' : ''}>${group.name}</option>
</c:forEach>
</select>
</td>
<td>
${row.permissionName}
</td>
<td>
<input type="button" onclick="updateUser('${row.id}')"/>
</td>
<td>
<input type="button" onclick="deleteUser('${row.id}')"/>
</td>
</tr>
</c:forEach>
</table>


<form:form modelAttribute="user" action="user/create">
Login: <form:input path="loginId"/><br/>
Email: <form:input path="email"/><br/>
Password: <form:password path="password"/><br/>
FirstName: <form:input path="firstName"/><br/>
LastName: <form:input path="lastName"/><br/>
Group: <form:select path="groupId" items="${groupList}" itemLabel="name" itemValue="id"/><br/>
Permission: <form:select path="permissionId" items="${permissionList}" itemLabel="name" itemValue="id"/>
<input type="submit"/>
</form:form>

<form id="deleteUserForm">
<input type="hidden" name="id"/>
</form>
<form id="updateUserForm">
<input type="hidden" name="id"/>
</form>

<jsp:include page="../common/footer.jsp"/>
</body>
</html>